<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      default: () => ({
        series: [
          { value: 335, name: '技术部' },
          { value: 310, name: '市场部' },
          { value: 234, name: '人事部' },
          { value: 135, name: '财务部' },
          { value: 148, name: '运营部' }
        ]
      })
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler() {
        this.updateChart()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.updateChart()
    },
    updateChart() {
      if (!this.chart) return
      
      const option = {
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)',
          textStyle: {
            fontSize: 14,
            color: '#fff'
          },
          backgroundColor: 'rgba(0, 0, 0, 0.8)',
          borderColor: 'rgba(255, 255, 255, 0.2)',
          borderWidth: 1
        },
        legend: {
          left: 'center',
          bottom: '5%',
          data: this.chartData.series.map(item => item.name),
          textStyle: {
            fontSize: 12,
            color: '#fff',
            fontWeight: 'bold'
          },
          itemWidth: 12,
          itemHeight: 12,
          itemGap: 15
        },
        series: [
          {
            name: '文档处理',
            type: 'pie',
            roseType: 'radius',
            radius: [20, 85], // 调整半径，为文字留出更多空间
            center: ['50%', '40%'],
            data: this.chartData.series,
            animationEasing: 'cubicInOut',
            animationDuration: 2600,
            label: {
              show: true,
              position: 'outside',
              formatter: '{b}\n{c}',
              fontSize: 12,
              color: '#fff',
              fontWeight: 'bold',
              lineHeight: 16
            },
            labelLine: {
              show: true,
              length: 15,
              length2: 10,
              lineStyle: {
                color: 'rgba(255, 255, 255, 0.6)',
                width: 1
              }
            },
            emphasis: {
              label: {
                fontSize: 14,
                fontWeight: 'bold'
              }
            }
          }
        ]
      }
      
      this.chart.setOption(option)
    }
  }
}
</script>
